package ReSofting.it.GoldDirector.Helpers;

import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import ReSofting.it.GoldDirector.Interfaces.IRemoteCallListener;
import ReSofting.it.GoldDirector.JsonTrans.JsonTransformer_myMenu;
import ReSofting.it.GoldDirector.Model.ReturnData;
import ReSofting.it.GoldDirector.Model.myMenu;
import android.os.AsyncTask;
import android.util.Log;



public class GetDataMenuAsync extends AsyncTask<String, Void, ReturnData< List<myMenu>> > {
	 
    private final static String TAG = GetDataAsync.class.getName();
    
    IRemoteCallListener<ReturnData< List<myMenu>> > listener;
 
    
    public GetDataMenuAsync(IRemoteCallListener<ReturnData< List<myMenu>> >  listener) {
        this.listener = listener;
    }
 
    @Override
    protected ReturnData< List<myMenu>>  doInBackground(String... urls) 
    {
    	HttpEntity entity = null;
        List<myMenu> oggettiList = new ArrayList<myMenu>();
        ReturnData< List<myMenu>>  _retData = null ;
        String url = urls[0];
        String _responseString = null;
        HttpResponse _httpResponse = null;
                
       // HttpHost host = new HttpHost("10.0.2.2" , 8096);
        //HttpGet get = new HttpGet( "/Services/OrdersSvc.svc/GetMenu" ); //url
        HttpParams httpParameters = new BasicHttpParams();
	     HttpConnectionParams.setConnectionTimeout(httpParameters, 55000);
	     HttpConnectionParams.setSoTimeout(httpParameters, 55000);
                             
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.setParams(httpParameters);
        
        HttpGet request = new HttpGet(url);//8096
 
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");
               
        //oggettiList.add("Uri: "+ request.getURI());
        
        try {
        	        	
        	_httpResponse = httpClient.execute(request);   
        	
        	if(_httpResponse != null)
             _responseString =   EntityUtils.toString(_httpResponse.getEntity());      
             
            if(_httpResponse == null | _responseString == null)
            {
            	 _retData = new ReturnData< List<myMenu> >  (
            			 9000, null,true,"response data is null", null);
            	 
            	 return _retData; //return
            }
            else 
            {
            	 _retData = new ReturnData< List<myMenu> >  (
            			 _httpResponse.getStatusLine().getStatusCode(),
            			 _httpResponse.getAllHeaders(), false,"", oggettiList  );
            }
            	/* 
        	 if(response.getStatusLine().getStatusCode() != 200)
             {
        		 resVarie.Alert2("Errore connessione rete","Code : "+ response.getStatusLine().getStatusCode(), this.);
             }
             */
             
            if(_responseString == null)
            	return _retData;
                                  
            	JsonTransformer_myMenu jsonner = new JsonTransformer_myMenu();
                JSONArray rawPersonaList = new JSONArray(_responseString);
                 
                JSONObject jsObj = null ;                 
                myMenu dato = null ;
                
                 for(int i = 0; i < rawPersonaList.length(); i++) {

                	 jsObj = rawPersonaList.getJSONObject(i);             
                	 dato = jsonner.jsonTo_myMenu(jsObj);
                 	 oggettiList.add(dato);
                 }

        }
                 catch(SocketException ex)
                 {
                   Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
                     ex.printStackTrace();
                     
                    oggettiList.add( new myMenu( UUID.randomUUID() ,"",  "Error : "+ ex.getMessage()) );
                 }
             
         catch (Exception e) {
            Log.d(TAG, e.getMessage());
            
            oggettiList.add(new myMenu(UUID.randomUUID() , "", "Error : "+ e.getMessage()));
            
            e.printStackTrace();            
            
        }
       
        return _retData;
    }
 
    protected void onPostExecute(ReturnData< List<myMenu>>  retData) {
       listener.onRemoteCallComplete(retData);
    }
 
}
